package com.kproduce.roundcorners;

import com.kproduce.roundcorners.util.RoundHelper;
import com.kproduce.roundcorners.util.RoundMethodInterface;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class RoundImageView extends Image implements Component.DrawTask, RoundMethodInterface {
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00200, "RoundImageView");

    private RoundHelper mHelper = new RoundHelper();

    private Paint mPaint;

    private Path mPath;

    private double mStrokeWidth;

    private RectFloat rectFloat;

    private float mStrokeRadii[];

    public RoundImageView(Context context) {
        super(context);
    }

    public RoundImageView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(attrSet);
    }

    public RoundImageView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    private void init(AttrSet attrSet) {
        mStrokeRadii = new float[8];
        mPaint = new Paint();
        mPath = new Path();
        rectFloat = new RectFloat();
        mStrokeWidth = 10;
    }

    public void setPixelMapToRoundedRect(int resId, int topLeft, int topRigth, int bottomRight, int bottomLeft) {
        setPixelMap(resId);
        setClipGravity(GRAVITY_CENTER);
        mStrokeRadii[0] = mStrokeRadii[1] = topLeft;
        mStrokeRadii[2] = mStrokeRadii[3] = topRigth;
        mStrokeRadii[4] = mStrokeRadii[5] = bottomRight;
        mStrokeRadii[6] = mStrokeRadii[7] = bottomLeft;
        setCornerRadii(mStrokeRadii);
        setScaleMode(ScaleMode.CLIP_CENTER);
    }

    private void drawLineWhitStroke(Canvas canvas, int width, int height, Color color) {
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setStrokeWidth((float) mStrokeWidth);
        mPaint.setColor(color);
        mPath.reset();
        rectFloat.fuse((float) (mStrokeWidth / 2), (float)(mStrokeWidth / 2), (float)(width - mStrokeWidth / 2), (float) (height - mStrokeWidth / 2));
        mPath.addRoundRect(rectFloat, mStrokeRadii, Path.Direction.COUNTER_CLOCK_WISE);
        canvas.drawPath(mPath, mPaint);
    }

    public void changeStrokeWidth(int strokeWidth) {
        this.mStrokeWidth = strokeWidth;
        invalidate();
    }

    @Override
    public void invalidate() {
        addDrawTask(this::onDraw);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
    }

    @Override
    public void setRadius(float radiusDp) {
        mHelper.setRadius(radiusDp);
    }

    @Override
    public void setRadius(float radiusTopLeftDp, float radiusTopRightDp, float radiusBottomLeftDp, float radiusBottomRightDp) {
        mHelper.setRadius(radiusTopLeftDp, radiusTopRightDp, radiusBottomLeftDp, radiusBottomRightDp);
    }

    @Override
    public void setRadiusLeft(float radiusDp) {
        mHelper.setRadiusLeft(radiusDp);
    }

    @Override
    public void setRadiusRight(float radiusDp) {
        mHelper.setRadiusRight(radiusDp);
    }

    @Override
    public void setRadiusTop(float radiusDp) {
        mHelper.setRadiusTop(radiusDp);
    }

    @Override
    public void setRadiusBottom(float radiusDp) {
        mHelper.setRadiusBottom(radiusDp);
    }

    @Override
    public void setRadiusTopLeft(float radiusDp) {
        mHelper.setRadiusTopLeft(radiusDp);
    }

    @Override
    public void setRadiusTopRight(float radiusDp) {
        mHelper.setRadiusTopRight(radiusDp);
    }

    @Override
    public void setRadiusBottomLeft(float radiusDp) {
        mHelper.setRadiusBottomLeft(radiusDp);
    }

    @Override
    public void setRadiusBottomRight(float radiusDp) {
        mHelper.setRadiusBottomRight(radiusDp);
    }

    @Override
    public void setStrokeWidth(float widthDp) {
        mHelper.setStrokeWidth(widthDp);
    }

    @Override
    public void setStrokeColor(int color) {
        mHelper.setStrokeColor(color);
    }

    @Override
    public void setStrokeWidthColor(float widthDp, int color) {
        mHelper.setStrokeWidthColor(widthDp, color);
    }
}
